﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Uniloader.Interface;
using Uniloader.Managers;
using System.IO;
using MonoTorrent.Client;
using MonoTorrent.Common;
using System.Net;
using System.ComponentModel;
using System.Windows.Threading;

namespace XamlTorrent.Managers.MonoTorrent.UI
{
    /// <summary>
    /// Interaction logic for CreateTorrentTask.xaml
    /// </summary>
    public partial class TorrentTaskProperties : UserControl 
    {
        public TorrentTaskProperties()
        {
            InitializeComponent();
        }

        #region Properties
        public static readonly DependencyProperty LoadedTorrentProperty = DependencyProperty.Register("LoadedTorrent", typeof(Torrent), typeof(CreateTorrentTask));
        /// <summary>
        /// Currently loaded torrent file.
        /// </summary>
        protected Torrent LoadedTorrent
        {
            get { return (Torrent)this.GetValue(LoadedTorrentProperty); }
            set
            {
                this.SetValue(LoadedTorrentProperty, value);

                // Update the SelectedFilesCount, all initially selected.
                if (value != null)
                    this.SetValue(SelectedFilesCountProperty, value.Files.Length);
                else
                    this.SetValue(SelectedFilesCountProperty, 0);
            }
        }

        public static readonly DependencyProperty SelectedFilesCountProperty = DependencyProperty.Register("SelectedFilesCount", typeof(int), typeof(CreateTorrentTask));
        /// <summary>
        /// Number of files selected for download withing the torrent.
        /// </summary>
        protected int SelectedFilesCount
        {
            get { return (int)this.GetValue(SelectedFilesCountProperty); }
        } 
        #endregion

        /// <summary>
        /// Updates SelectedFilesCount property when a checkbox in the file list is checked/unchecked.
        /// </summary>
        /// <param name="sender">CheckBox whose status changed.</param>
        private void FileCheckBox_CheckedChanged(object sender, RoutedEventArgs e)
        {
            CheckBox checkBox = (CheckBox)sender;

            if (checkBox.IsChecked ?? false)
                this.SetValue(SelectedFilesCountProperty, SelectedFilesCount + 1);
            else
                this.SetValue(SelectedFilesCountProperty, SelectedFilesCount - 1);
        }
    }
}
